package com.xyht.sca_s.student_manage_system.modules.assetConsumable.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.entity.SmsAssetConsumableInStorageApply;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.entity.req.SmsAssetConsumableInStorageApplyHistoryReq;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.entity.req.consumableInStorageApply.SmsAssetConsumableInStorageApplyAllowReq;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.entity.req.consumableInStorageApply.SmsAssetConsumableInStorageApplyReq;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.service.SmsAssetConsumableInStorageApplyService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 低值易耗品入库申请 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-06-25
 */
@RestController
@RequestMapping("/assetConsumable/inStorageApply")
@SecretBody
public class SmsAssetConsumableInStorageApplyController {
    @Autowired
    private SmsAssetConsumableInStorageApplyService smsAssetConsumableInStorageApplyService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty(value = "提交入库申请")
    @PostMapping("/add")
    public ResponseResult addConsumableInStorageApply(@RequestBody @Validated SmsAssetConsumableInStorageApplyReq req, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetConsumableInStorageApplyService.addConsumableInStorageApply(req, userId);
    }

    @ApiModelProperty(value = "撤销入库申请")
    @PostMapping("/cancel")
    public ResponseResult cancelConsumableInStorageApply(@RequestBody SmsAssetConsumableInStorageApplyReq cancelReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetConsumableInStorageApplyService.cancelConsumableInStorageApply(cancelReq, userId);
    }

    @ApiModelProperty(value = "审批列表")
    @GetMapping("/list")
    public ResponseResult getAssetConsumableInStorageApplyList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                               @RequestParam(value = "applyType") Integer applyType,
                                                               HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetConsumableInStorageApplyService.getAssetConsumableInStorageApplyList(userId, applyType, pageNum, pageSize);
    }

    @ApiModelProperty(value = "审批详情")
    @GetMapping("/detail")
    public ResponseResult getAssetConsumableInStorageApplyDetail(@RequestParam(value = "applyId") String applyId, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetConsumableInStorageApplyService.getAssetConsumableInStorageApplyDetail(applyId, userId);
    }

    @ApiModelProperty(value = "审批")
    @PostMapping("/allow")
    public ResponseResult allowAssetConsumableInStorageApply(@RequestBody SmsAssetConsumableInStorageApplyAllowReq allowReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetConsumableInStorageApplyService.allowAssetConsumableInStorageApply(allowReq, userId);
    }

    @ApiModelProperty(value = "表格下载")
    @GetMapping("/download")
    public ResponseResult tableDownload(@RequestParam("applyId") String applyId, HttpServletResponse response) {
        ResponseResult responseResult;
        try {
            Future<ResponseResult> future = smsAssetConsumableInStorageApplyService.tableDownload(applyId, response);
            while (true) {
                if (future.isDone()) {
                    break;
                }
            }
            responseResult = future.get();
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiModelProperty(value = "入库申请记录导出")
    @PostMapping("/exportAllApplyHistory")
    public ResponseResult exportAllApplyList(@RequestBody SmsAssetConsumableInStorageApplyHistoryReq historyReq, HttpServletResponse response) {
        ResponseResult responseResult;
        try {
            Future<ResponseResult> future = smsAssetConsumableInStorageApplyService.exportAllApplyList(historyReq, response);
            while (true) {
                if (future.isDone()) {
                    break;
                }
            }
            responseResult = future.get();
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiModelProperty(value = "获取所有审批列表")
    @GetMapping("/allList")
    public ResponseResult getAssetConsumableInStorageApplyAllList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                  @RequestParam(value = "allowStatus", required = false) Integer allowStatus,
                                                                  @RequestParam(value = "searchStr", required = false) String searchStr,
                                                                  @RequestParam(value = "applyName", required = false) String applyName) {
        return smsAssetConsumableInStorageApplyService.getAssetConsumableInStorageApplyAllList(allowStatus, searchStr, applyName, pageNum, pageSize);
    }

    @ApiOperation(value = "预警提醒")
    @GetMapping("/warningReminder")
    public ResponseResult warningReminder(@RequestParam(value = "searchStr", required = false)String searchStr) {
        return smsAssetConsumableInStorageApplyService.warningReminder(searchStr);
    }
}

